﻿using System.Collections;
using Config;
using UnityEngine;

namespace Component.Tools
{
    public class ClockTools
    {
        private Transform pointer;
        private Task runTask;

        public delegate void Clocking(bool runCycle);

        public Clocking DayLaterEvent;

        public ClockTools()
        {
            GameObject gameObject = Resources.Load<GameObject>("Prefabs/Clock");
            pointer = gameObject.transform.GetChild(0);
        }

        public ClockTools(Transform p)
        {
            pointer = p;
            runTask = new Task(TimePass(), false);
        }

        private IEnumerator TimePass()
        {
            int currentAngle = 0;
            
            while (true)
            {
                pointer.rotation = Quaternion.AngleAxis(currentAngle--, Vector3.forward);
                if (Mathf.Abs(currentAngle) % 720 == 0)
                {
                    DayLaterEvent.Invoke(true);
                }
                
                yield return new WaitForSeconds(TimeScale.TIME_SCALE);
            }
        }

        public void PauseRunning()
        {
            runTask.Pause();
        }

        public void ContinueRunning()
        {
            if (!runTask.Running)
            {
                runTask.Start();
            }
            runTask.Continue();
        }

        public void Reset()
        {
            runTask = new Task(TimePass());
            runTask.Pause();
        }
        
    }

}